/*
** env.c for 42sh in /home/demesy_j/svn/quarante-deuxsh/trunk
** 
** Made by julien demesy
** Login   <demesy_j@epitech.net>
** 
** Started on  Mon Mar 12 16:27:20 2012 julien demesy
** Last update Tue Mar 13 15:59:25 2012 julien demesy
*/

#include	<stdlib.h>
#include	<stdio.h>
#include	<string.h>
#include	"list.h"
#include	"builtins.h"

void		my_setenv(t_list *env, char *str)
{
  add_end(env, strdup(str));
}

void		my_unsetenv(t_list *env, char *var)
{
  t_list	*it;

  it = env->next;
  while (strncmp(var, it->data, strlen(var)) != 0 && it != env)
    it = it->next;
  if (it == env)
    printf("ERROR : Variable %s not found.\n", var);
  else
    delete_elem(it);
}

void		my_env(t_list *env)
{
  t_list	*it;

  it = env->next;
  while (it != env)
    {
      puts(it->data);
      it = it->next;
    }
}

char		*my_getenv(t_list *env, char *var)
{
  t_list	*it;
  int		i;
  char		*ret;
  int		i2;

  it = env->next;
  while (strncmp(it->data, var, strlen(var)) != 0 && it != env)
    it = it->next;
  if (it == env)
    return (NULL);
  i = 0;
  while (it->data[i++] != '=');
  i2 = i;
  while (it->data[i++]);
  ret = malloc((i - i2) * sizeof(char));
  if (ret == NULL)
    my_exit(-1);
  i = 0;
  while (it->data[i2])
    ret[i++] = it->data[i2++];
  ret[i] = '\0';
  return (ret);
}
